package com.igd.business.service.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.igd.business.dao.IRoleDao;
import com.igd.business.service.IRoleService;
import com.igd.common.constant.IConstant;
import com.igd.exceptions.IgdException;
import com.igd.pojo.vo.PageList;
import com.igd.security.RoleResource;
import com.igd.security.Roles;
import com.igd.security.User;
import com.igd.utils.CommonUtil;
import com.igd.utils.DateUtil;

/**
 * @createDate 2013-7-22
 *
 * @author QIAOYU
 *
 * @version 1.0
 *
 * @description 
 */

public class IRoleServiceImpl implements IRoleService {
	
	private IRoleDao roleDao;
	
	public List<RoleResource> getRoleResource(PageList pageList) throws SQLException,IgdException {

		return this.getRoleDao().queryRoleResource(pageList,new RoleResource());
	}
	
	public List<Roles> getAllRole(PageList pageList) throws SQLException,IgdException {
		
		return this.getRoleDao().queryAllRole(pageList,new Roles());
	}

	public Roles getRoleInfoByRoleId(String roleId) throws SQLException,IgdException {
		
		if(StringUtils.isNotEmpty(roleId)){
			
			Roles role = new Roles();
			
			role.setRolesId(roleId);
			
			return this.getRoleDao().queryRoleInfo(role);

		}else{
			
			throw new IgdException("role.notFoundRoleInfo");

		}
	}

	public void delRoleByRoleId(String roleId) throws SQLException,IgdException {
		
		if(StringUtils.isNotEmpty(roleId)){

			Roles role = new Roles();
			
			role.setRolesId(roleId);
			
			this.getRoleDao().deleteRole(role);

		}
	}

	public void delRoleResourceByRoleResId(String roleResId) throws SQLException,IgdException{
		
		if(StringUtils.isNotEmpty(roleResId)){
			
			RoleResource roleRes = new RoleResource();
			
			roleRes.setRoleResId(roleResId);
			
			this.getRoleDao().deleteRoleResource(roleRes);
		}
	}

	public void updRoleByRoleId(Roles role) throws SQLException,IgdException {
		
		if(StringUtils.isNotEmpty(role.getRolesId())){
			
			this.getRoleDao().updRole(role);

		}
	}

	public Roles saveRole(Roles role) throws SQLException, IgdException {
		
		if(null != role){
			
			User user = (User) CommonUtil.getCurrentUser();
			
			if(null != user){
				
				role.setRolesStatus(IConstant.ROLE_NORMAL_STATUS);
				
				role.setRolesCreateTime(DateUtil.getLongDate());
				
				role.setRolesCreateUser(user.getUserId());
				
				this.getRoleDao().insertRole(role);
				
				return role;
				
			}
		}
		
		return null;
		
	}

	public boolean batchSaveRoleResource(List<RoleResource> roleRes) throws IgdException {
		
		if(null != roleRes && roleRes.size() > 0){
			
			return this.getRoleDao().batchInsertRoleResource(roleRes);

		}
		
		return Boolean.FALSE;
	}
	
	public IRoleDao getRoleDao() {
		return roleDao;
	}

	public void setRoleDao(IRoleDao roleDao) {
		this.roleDao = roleDao;
	}
}
